package com.dataporter.gather.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dataporter.gather.bed.Bed;
import com.dataporter.gather.bed.BedService;
import com.dataporter.gather.patient.Patient;
import com.dataporter.gather.patient.PatientService;
import com.dataporter.gather.server.Temperature;
import com.dataporter.gather.server.TemperatureService;
import com.touchwisdom.modular.util.config.Page;

@Controller
@RequestMapping("/bed")
public class BedController {
	@Resource
	private BedService bedService;

	@Resource
	private PatientService patientService;

	@Resource
	private TemperatureService temperatureService;

	@RequestMapping("/index.action")
	public String index(EasyUiPage<Bed> easyuiPage, Model model) {
		Page<Bed> page = this.bedService.findByPage(easyuiPage);

		Patient patient = null;
		Temperature temperature = null;
		for (Bed bed : page.getResult()) {
			patient = this.patientService.findByBedIdAndEndTimeIsNull(bed
					.getId());
			if (patient != null) {
				bed.setPatient(patient);
				// 获取温度信息
				if (patient.getEquipNo() != null) {
					temperature = this.temperatureService
							.findLatestByEquipNoAndMeasureTime(
									patient.getEquipNo(),
									patient.getStartTime());
					if (temperature != null) {
						bed.setTiwen(temperature);
					}
				}
			}
		}
		model.addAttribute("start", page.getFirst());
		model.addAttribute("page", page.getPageNo());
		model.addAttribute("rows", page.getPageSize());
		model.addAttribute("totalCount", page.getTotalCount());
		model.addAttribute("bedList", page.getResult());
		return "bed/index";
	}

	@RequestMapping("/edit.action")
	public String edit(String id, String type, Model model) {
		if (StringUtils.isNotBlank(id)) {
			Bed bed = this.bedService.getEntity(id);
			model.addAttribute("bed", bed.getPatient());
		}
		return "bed/" + type;
	}

	@RequestMapping("/outBed.action")
	@ResponseBody
	public String outBed(String ids, Model model) {
		this.bedService.outBed(ids);
		return Boolean.TRUE.toString().toLowerCase();
	}

	@ResponseBody
	@RequestMapping("/save.action")
	public String save(Bed bed) {
		this.bedService.saveEntity(bed);
		return Boolean.TRUE.toString().toLowerCase();
	}

	@ResponseBody
	@RequestMapping(value = "/show.action", produces = { "application/json;charset=UTF-8" })
	public String show(String id) {
		Bed bed = this.bedService.getEntity(id);
		String data = "\"\"";
		if (bed != null && bed.getPatient() != null) {
			data = String.format(
					"{\"equipNo\":\"%s\",\"name\":\"%s\",\"id\":\"%s\"}", bed
							.getPatient().getEquipNo(), bed.getPatient()
							.getName(), bed.getPatient().getId());
		}
		EasyUiMessage message = new EasyUiMessage();
		message.setData(data);
		return message.toString();
	}

	@ResponseBody
	@RequestMapping("/update.action")
	public String update(Patient patient, String startTimeStr, String endTimeStr) {
		if (StringUtils.isNotBlank(startTimeStr)) {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			try {
				patient.setStartTime(format.parse(startTimeStr));
			} catch (ParseException e) {
				patient.setStartTime(new Date());
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.patientService.saveEntity(patient);
		return Boolean.TRUE.toString().toLowerCase();
	}

	@ResponseBody
	@RequestMapping("/delete.action")
	public String delete(Bed bed) {
		this.bedService.deleteEntity(bed);
		return new EasyUiMessage().toString();
	}
}